//
//  PlayerManager.m
//  Leisure
//
//  Created by wenze on 16/4/6.
//  Copyright © 2016年 wenze. All rights reserved.
//

#import "PlayerManager.h"

@implementation PlayerManager

+ (instancetype)defaultManager{
    static PlayerManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PlayerManager alloc] init];
    });
    return manager;
}

- (instancetype)init{
    if (self = [super init]) {
        _playType = PlayTypeList;
        _playState = PlayerStatePause;
    }
    return self;
}

#pragma mark - 初始化数据源
- (void)setMusicArray:(NSMutableArray *)musicArray{
    //将播放原始列表置为空 保证不重复
    [_musicArray removeAllObjects];
    //赋值
    _musicArray = [musicArray mutableCopy];
    //创建播放的item
    AVPlayerItem *avItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:musicArray[_playIndex]]];
    //创建播放器
    if (_avPlayer) {
        //用新的替换掉原本的播放单元
        [_avPlayer replaceCurrentItemWithPlayerItem:avItem];
    }else{
        _avPlayer = [[AVPlayer alloc] initWithPlayerItem:avItem];
    }
    //设置默认暂停
    _playState = PlayerStatePause;
}

//获取当前播放时间
- (float)currentTime{
    //判断当前播放帧
    if (_avPlayer.currentItem.timebase == 0) {
        return 0;
    }
    //当前时间除以时间单位 得到时间
    return _avPlayer.currentTime.value / _avPlayer.currentTime.timescale;
}

//总时间
- (float)totalTime{
    if (_avPlayer.currentItem.duration.timescale == 0) {
        return 0;
    }
    return _avPlayer.currentItem.duration.value / _avPlayer.currentItem.duration.timescale;
}

#pragma mark - 播放方法的实现
//播放
- (void)play{
    [_avPlayer play];
    //记录当前的播放状态
    _playState = PlayerStatePlay;
}

//暂停
- (void)pause{
    [_avPlayer pause];
    _playState = PlayerStatePause;
}

//停止
- (void)stop{
    //回到开始
    [self seekToNewTime:0];
    [self pause];
}

//指定位置播放
- (void)seekToNewTime:(float)time{
    //获取当前时间
    CMTime newTime = _avPlayer.currentTime;
    //重新赋值
    newTime.value = newTime.timescale * time;
    //播放跳转
    [_avPlayer seekToTime:newTime];
}

//上一首
- (void)lastMusic{
    //判断播放模式
    if(_playType == PlayTypeRandom){
        _playIndex = arc4random() % _musicArray.count;
    }else{
        if (_playIndex == 0) { //如果是第一首
            _playIndex = _musicArray.count - 1;
        }else{
            _playIndex--;
        }
    }
    [self changeMusicWithIndex:_playIndex];
}

//下一首
- (void)nextMusic{
    if (_playType == PlayTypeRandom) {
        _playIndex = arc4random() % _musicArray.count;
    }else{
        if (_playIndex == _musicArray.count - 1) { //如果是最后一首
            _playIndex = 0;
        }else{
            _playIndex++;
        }
    }
    [self changeMusicWithIndex:_playIndex];
}

//改变当前的播放单元
- (void)changeMusicWithIndex:(NSInteger)index{
    //指定位置进行赋值操作
    _playIndex = index;
    AVPlayerItem *avItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:_musicArray[index]]];
    [_avPlayer replaceCurrentItemWithPlayerItem:avItem];
    [self play];
}

//播放完成
- (void)playDidFinish{
    if (_playType == PlayTypeSingle) {
        //单曲播放
        [self stop];
        //单曲循环
//        [self changeMusicWithIndex:_playIndex];
    }else{
        [self nextMusic];
        NSString *string = @"123";
        // 当播放完成时,将消息传出,用于UI的处理
        [[NSNotificationCenter defaultCenter]postNotificationName:@"playDidFinish" object:string];
    }
}

@end
